package com.stable.web.controller;

import com.stable.service.NewFinanceService;
import com.stable.utils.DateUtil;
import com.stable.utils.FileUtil;
import com.stable.vo.bus.NewFinanceReturnInfo;
import com.stable.vo.http.JsonResult;
import com.stable.vo.spi.req.EsQueryPageReq;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 财务数据分析
 */
@RequestMapping("/finance")
@RestController
@Log4j2
public class FinanceController {

    @Autowired
    private NewFinanceService newFinanceService;


    /**
     * 最近几年的财务数据分析
     * 只是看ROE和市盈率还不够，做中长线，参考的范围还很大，比如最近三到五年毛利率＞30%，ROE＞15%，现金含量＞80%，负债率＜60%等其实非常重要。
     *
     * @param last1YyzsrtbzzPercent 最近1年的财务营业总收入同比增长
     * @param last2YyzsrtbzzPercent 最近2年的财务营业总收入同比增长
     * @param last3YyzsrtbzzPercent 最近3年的财务营业总收入同比增长
     * @param last1JqjzcsylPercent  最近1年的财务加权净资产收益率(%)
     * @param last2JqjzcsylPercent  最近2年的财务加权净资产收益率(%)
     * @param last3JqjzcsylPercent  最近3年的财务加权净资产收益率(%)
     * @param zcfzl;                资产负债率(%)
     * @return
     */
    @RequestMapping(value = "/financeQuery", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> stocklist(@RequestParam(name = "last1YyzsrtbzzPercent", defaultValue = "19", required = false) double last1YyzsrtbzzPercent,
                                                @RequestParam(name = "last2YyzsrtbzzPercent", defaultValue = "19", required = false) double last2YyzsrtbzzPercent,
                                                @RequestParam(name = "last3YyzsrtbzzPercent", defaultValue = "19", required = false) double last3YyzsrtbzzPercent,
                                                @RequestParam(name = "last1JqjzcsylPercent", defaultValue = "14", required = false) double last1JqjzcsylPercent,
                                                @RequestParam(name = "last2JqjzcsylPercent", defaultValue = "10", required = false) double last2JqjzcsylPercent,
                                                @RequestParam(name = "last3JqjzcsylPercent", defaultValue = "9", required = false) double last3JqjzcsylPercent,
                                                @RequestParam(name = "zcfzl", defaultValue = "66", required = false) double zcfzl,
                                                String code,
                                                String conceptName,  //相关概念
                                                String gsld,  //公司亮点
                                                EsQueryPageReq queryPage) {
        queryPage.setPageSize(1000);
        if (StringUtils.isEmpty(queryPage.getSortName())) {
            queryPage.setSortName("dateMilli");
        }
        String queryDate = DateUtil.getTodayYYYYMMDD();
        JsonResult r = new JsonResult();
        try {
            r.setResult(newFinanceService.queryFinance(queryDate, code, last1YyzsrtbzzPercent, last2YyzsrtbzzPercent, last3YyzsrtbzzPercent, last1JqjzcsylPercent, last2JqjzcsylPercent, last3JqjzcsylPercent, queryPage, conceptName, zcfzl, gsld));
            r.setStatus(JsonResult.OK);
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus(JsonResult.ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }


    /**
     * 下载文件
     *
     * @return
     */
    @RequestMapping({"/download"})
    public void download(@RequestParam(name = "last1YyzsrtbzzPercent", defaultValue = "19", required = false) double last1YyzsrtbzzPercent,
                         @RequestParam(name = "last2YyzsrtbzzPercent", defaultValue = "19", required = false) double last2YyzsrtbzzPercent,
                         @RequestParam(name = "last3YyzsrtbzzPercent", defaultValue = "19", required = false) double last3YyzsrtbzzPercent,
                         @RequestParam(name = "last1JqjzcsylPercent", defaultValue = "14", required = false) double last1JqjzcsylPercent,
                         @RequestParam(name = "last2JqjzcsylPercent", defaultValue = "10", required = false) double last2JqjzcsylPercent,
                         @RequestParam(name = "last3JqjzcsylPercent", defaultValue = "9", required = false) double last3JqjzcsylPercent,
                         @RequestParam(name = "zcfzl", defaultValue = "66", required = false) double zcfzl,
                         String code,
                         String conceptName,
                         String gsld,
                         EsQueryPageReq queryPage) {
        queryPage.setPageSize(1000);
        if (StringUtils.isEmpty(queryPage.getSortName())) {
            queryPage.setSortName("dateMilli");
        }
        String queryDate = DateUtil.getTodayYYYYMMDD();
        JsonResult r = new JsonResult();
        List<NewFinanceReturnInfo> newFinanceReturnInfos = newFinanceService.queryFinance(queryDate, code, last1YyzsrtbzzPercent, last2YyzsrtbzzPercent, last3YyzsrtbzzPercent, last1JqjzcsylPercent, last2JqjzcsylPercent, last3JqjzcsylPercent, queryPage, conceptName, zcfzl, gsld);

        String filename = "财务3年分析列表.txt";
        FileUtil.downloadFile(newFinanceReturnInfos.stream().map(NewFinanceReturnInfo::getCode).collect(Collectors.toList()), filename);
    }

}
